<?php

/**
 * This is the model class for table "zy_tvchannel".
 *
 * The followings are the available columns in table 'zy_tvchannel':
 * @property integer $channel_id
 * @property integer $tv_id
 * @property string $channel_name
 * @property string $channel_url
 * @property integer $is_watch
 * @property integer $sort
 * @property string $price
 * @property string $tv_img
 * @property integer $is_delete
 * @property string $crate_time
 * @property string $crate_user
 * @property string $update_time
 * @property string $update_user
 */
class Tvchannel extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_tvchannel';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tv_id, is_watch, sort, is_delete', 'numerical', 'integerOnly'=>true),
			array('channel_name', 'length', 'max'=>300),
			array('channel_url, tv_img', 'length', 'max'=>500),
			array('price', 'length', 'max'=>11),
			array('crate_user, update_user', 'length', 'max'=>50),
			array('crate_time, update_time', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('channel_id, tv_id, channel_name, channel_url, is_watch, sort, price, tv_img, is_delete, crate_time, crate_user, update_time, update_user', 'safe', 'on'=>'search'),
			array('tv_img', 'file',
			      'allowEmpty'=>true,
			      'types'=>'jpg, gif, png',
			      'maxSize'=>1024 * 1024 * 1, 
			      'tooLarge'=>'文件最大不超过1MB，请重新上传文件',
			      ),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'channel_id' => Yii::t('base','ChannelId'),
			'tv_id' => Yii::t('base','Tv Category'),
			'channel_name' => Yii::t('base','Channel Name'),
			'channel_url' => Yii::t('base','Channel Url'),
			'is_watch' => Yii::t('base','Is Watch'),
			'sort' => Yii::t('base','Sort'),
			'price' => Yii::t('base','Price'),
		'tv_img'=>Yii::t('base','Tv Img'),
			'is_delete' => 'Is Delete',
			'crate_time' => 'Crate Time',
			'crate_user' => 'Crate User',
			'update_time' => 'Update Time',
			'update_user' => 'Update User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('channel_id',$this->channel_id);
		$criteria->compare('tv_id',$this->tv_id);
		$criteria->compare('channel_name',$this->channel_name,true);
		$criteria->compare('channel_url',$this->channel_url,true);
		$criteria->compare('is_watch',$this->is_watch);
		$criteria->compare('sort',$this->sort);
		$criteria->compare('price',$this->price,true);
		$criteria->compare('tv_img',$this->tv_img,true);
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('crate_time',$this->crate_time,true);
		$criteria->compare('crate_user',$this->crate_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user',$this->update_user,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Tvchannel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * 获取TV分类
	 */
	public function getTvShowTvId(){
		$sql = "select * from zy_tvshow";
		$results  = Yii::app()->db->createCommand($sql)->queryAll();
		return CHtml::listData($results,'tv_id','tv_name');
	}
	
	/**
	 * 获取TV分类名称
	 */
	public function getTvShowTvName($tvid){
		$sql = "select * from zy_tvshow where tv_id=".$tvid;
		$results  = Yii::app()->db->createCommand($sql)->queryAll();
		return $results[0]['tv_name'];
	}
	
	
	
}
